package com.qitianyong.sgkusbcamera;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;

import com.gkusbsdk.GKUSBSDK;
import com.ligo.log.util.ZyLog;
import com.qitianyong.selfclass.BUS_MESSAGE;
import com.qitianyong.selfclass.BaseActivityEvent;
import com.qitianyong.selfclass.FindDeviceThread;
import com.qitianyong.selfclass.GKDevice;
import com.qitianyong.selfclass.LogUtils;
import com.qitianyong.selfclass.UsbDeviceFilter;



import static com.qitianyong.selfclass.CustomApp.__lock;
import static com.qitianyong.selfclass.GKDevice.__hDevice;
import static com.qitianyong.selfclass.GKDevice.usbConnection;

import org.greenrobot.eventbus.EventBus;

public class USBAttachReceiver extends BroadcastReceiver {

    private UsbDevice device;
    private Context context;

    @Override
    public void onReceive(final Context context, Intent intent) {
        boolean isGKUSBDevice = true;
        Parcelable parcelableExtra = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
        ZyLog.d("action = " + intent.getAction());
        if (parcelableExtra instanceof UsbDevice) {
            device = (UsbDevice) parcelableExtra;
            try {
                this.context = context;
                isGKUSBDevice = UsbDeviceFilter.getMatchingHostDevice(this.context, R.xml.device_filter, device);
                ZyLog.d("isGKUSBDevice = " + isGKUSBDevice);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (isGKUSBDevice) {
            LogUtils.addLog(intent.getAction());
            if (TextUtils.equals(intent.getAction(), UsbManager.ACTION_USB_DEVICE_DETACHED)) {
                if ((GKDevice.__device == null)) {
                    if (__hDevice != 0) {
                        GKUSBSDK.GKUSB_Destroy(__hDevice);
                    }
                    __hDevice = 0;
                } else {
                    __lock.lock();
                    ZyLog.d("disconnect---1");
                    if (__hDevice != 0) {
                        GKDevice.openStreamStatus = false;
                        GKUSBSDK.GKUSB_CloseStream(__hDevice);
                        GKUSBSDK.GKUSB_Destroy(__hDevice);
                        __hDevice = 0;
                        if (usbConnection != null) {
                            usbConnection.close();
                            usbConnection = null;
                        }
                        if ((FindDeviceThread.lastConnectTime - System.currentTimeMillis()) < 10000) {
                            FindDeviceThread.getInstance().setIntent(FindDeviceThread.OPEN_APP);
                        }
                        GKDevice.getInstance().notifyEmptyBusMessage(BUS_MESSAGE.BUS_OFFLINE);
                    }
                    __lock.unlock();
                }
                EventBus.getDefault().post(new BaseActivityEvent(BaseActivityEvent.DETACHUSB));
            } else if (TextUtils.equals(intent.getAction(), UsbManager.ACTION_USB_DEVICE_ATTACHED)) {
                FindDeviceThread.getInstance().setIntent(FindDeviceThread.OPEN_APP);
            }
        }
    }
}
